﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using zj.BLL;
using zj.Models;
using System.IO;
using System.IO.Ports;
using zj.Common;

namespace ProjectConfig.View.PopForms
{
    public partial class FrmEquipments : Form
    {
        private SerialPort serialPort = new SerialPort();
        private Projects project = null;
        private EquipmentsManager equipmentsManager = new EquipmentsManager();
        public FrmEquipments()
        {
            InitializeComponent();
            //整体复位
            GroupBoxReset();

            cmb_SerialNumber.Items.AddRange(SerialPort.GetPortNames());
            cmb_StopBit.Items.AddRange(Enum.GetNames(typeof(StopBits)));
            cmb_Parity.Items.AddRange(Enum.GetNames(typeof(Parity)));
            //绑定设备类型
            this.cmb_EquipmentType.DataSource = equipmentsManager.GetAllEtype();
            this.cmb_EquipmentType.DisplayMember = "EtypeName";
            this.cmb_EquipmentType.ValueMember = "EtypeId";
            this.cmb_EquipmentType.SelectedIndex = -1;
            //如果有控件级联查询，下一级的事件绑定要在上一级初始化完成后才进行
            this.cmb_EquipmentType.SelectedIndexChanged += new System.EventHandler(this.cmb_EquipmentType_SelectedIndexChanged);

            this.cmb_SerialNumber.SelectedIndex = 0;
            this.cmb_Baudrate.SelectedIndex = 0;
            this.cmb_DataBit.SelectedIndex = 0;
            this.cmb_Parity.SelectedIndex = 0;
            this.cmb_StopBit.SelectedIndex = 0;


        }

        public FrmEquipments(Projects value) : this()
        {
            this.project = value;
            this.lbl_ProjectName.Text = this.project.ProjectName;
        }
        /// <summary>
        /// 保存数据信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_SaveDevice_Click(object sender, EventArgs e)
        {
            //数据验证
            if (txt_EquipmentName.Text.Trim().Length == 0)
            {
                MessageBox.Show("请输入设备名称！", "提示信息：", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txt_EquipmentName.Focus();
                return;
            }
            if (cmb_EquipmentType.SelectedIndex == -1)
            {
                MessageBox.Show("请选择设备类型！", "提示信息：", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (cmb_ProtocalType.SelectedIndex == -1)
            {
                MessageBox.Show("请选择协议类型！", "提示信息：", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            //如果是以太网类型
            if (Convert.ToInt32(cmb_EquipmentType.SelectedValue) == 10)
            {
                if (txt_IPAddress.Text.Trim().Length == 0 || txt_Port.Text.Trim().Length == 0)
                {
                    MessageBox.Show("请填写IP地址和端口号！", "提示信息：", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txt_IPAddress.Focus();
                    return;
                }
                if (DataValidate.IsIPAddress(txt_IPAddress.Text.Trim()) || DataValidate.IsPort(txt_Port.Text.Trim()))
                {
                    MessageBox.Show("请填写正确的IP地址和端口号！", "提示信息：", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txt_IPAddress.SelectAll();
                    return;
                }

            }
            else if(Convert.ToInt32(cmb_EquipmentType.SelectedValue) == 11)
            {
                if(this.cmb_SerialNumber.SelectedIndex == -1)
                {
                    MessageBox.Show("请填写正确的串口号！", "提示信息：", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            else if(Convert.ToInt32(cmb_EquipmentType.SelectedValue) == 12)
            {
                if (txt_OPCServerName .Text.Trim().Length == 0 || txt_OPCNodeName .Text.Trim().Length == 0)
                {
                    MessageBox.Show("请填写OPC信息！", "提示信息：", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txt_OPCServerName.Focus();
                    return;
                }
            }

        }

        private void cmb_EquipmentType_SelectedIndexChanged(object sender, EventArgs e)
        {
            //分组框整体复位
            GroupBoxReset();
            //根据选择显示
            if (this.cmb_EquipmentType.Text.ToString().Contains("以太网"))
            {
                grb_Internet.Enabled = true;
                grb_Internet.BackColor = Color.FromArgb(146, 160, 166);

            }
            else if (this.cmb_EquipmentType.Text.ToString().Contains("串口"))
            {
                grb_SerialPort.Enabled = true;
                grb_SerialPort.BackColor = Color.FromArgb(146, 160, 166);

            }
            else if (this.cmb_EquipmentType.Text.ToString().Contains("OPC"))
            {
                grb_OPC.Enabled = true;
                grb_OPC.BackColor = Color.FromArgb(146, 160, 166);

            }
            //设备类型选项更改后触发协议类型选项的变化
            int eTypeId = Convert.ToInt32(this.cmb_EquipmentType.SelectedValue);
            this.cmb_ProtocalType.DataSource = equipmentsManager.GetProtocalTypesByEtypeId(eTypeId);
            this.cmb_ProtocalType.DisplayMember = "PtypeName";
            this.cmb_ProtocalType.ValueMember = "PtypeId";
            this.cmb_ProtocalType.SelectedIndex = 0;

        }
        private void GroupBoxReset()
        {
            //整体复位
            grb_SerialPort.BackColor = Color.FromArgb(192, 255, 255);
            grb_OPC.BackColor = Color.FromArgb(192, 255, 255);
            grb_Internet.BackColor = Color.FromArgb(192, 255, 255);
            grb_SerialPort.Enabled = grb_Internet.Enabled = grb_OPC.Enabled = false;
        }

        private void FrmEquipments_Load(object sender, EventArgs e)
        {

        }
    }
}
